package com.flymobi.mshop.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;
import android.widget.AdapterView.OnItemClickListener;

import com.flymobi.mshop.R;
import com.flymobi.mshop.adapter.MyGallery;

public class Home extends Common implements OnTouchListener, OnGestureListener {
	private ViewFlipper flipper;
	private GestureDetector mGestureDetector;
	private ImageSwitcher imageSwitcher;
	private static final Integer[] imagelist = { R.drawable.ad1,
			R.drawable.ad2, R.drawable.ad3, R.drawable.ad4, R.drawable.ad5 };
	private TextView title, s;
	private AutoCompleteTextView find;
	private RelativeLayout miaosha, tuangou, qianggou, miaosha_content;
	private LinearLayout qianggou_content;
	private Gallery gallery;
	private ImageView qianggou_img, miaosha_img;
	ImageView image;
	private Button find_bt;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.home);
		title = (TextView) findViewById(R.id.titleText);
		title.setText("移动商城");
		iniView();
		setListener();

	}

	public void iniView() {
		//倒计时
		s = (TextView) findViewById(R.id.app_product_countdown_ss);
		new CountDownTimer(59000, 1000) {

			public void onTick(long millisUntilFinished) {
				s.setText("" + millisUntilFinished / 1000);
			}

			public void onFinish() {
				s.setText("0");
			}
		}.start();
		
		find_bt = (Button) findViewById(R.id.home_search_button);
		find = (AutoCompleteTextView) findViewById(R.id.homeActivity_autoComplete);
		mGestureDetector = new GestureDetector(this);
		flipper = (ViewFlipper) findViewById(R.id.home_imageswitcher);

		for (int i = 0; i < 5; i++) {
			flipper.addView(getImgView(i));
		}

		flipper.setInAnimation(AnimationUtils.loadAnimation(Home.this,
				R.anim.right_in));
		flipper.setOutAnimation(AnimationUtils.loadAnimation(Home.this,
				R.anim.left_out));
		flipper.setOnTouchListener(this);
		// 允许长按住ViewFlipper,这样才能识别拖动等手势
		flipper.setLongClickable(true);
		flipper.startFlipping();
		
		miaosha = (RelativeLayout) findViewById(R.id.miaosha_relativeLayout_activities);
		qianggou = (RelativeLayout) findViewById(R.id.qianggou_title);
		tuangou = (RelativeLayout) findViewById(R.id.tuangou_relativeLayout_activities);
		qianggou_content = (LinearLayout) findViewById(R.id.qianggou_content);
		miaosha_content = (RelativeLayout) findViewById(R.id.miaosha_content);
		gallery = (Gallery) findViewById(R.id.home_crazy_gallery);
		gallery.setAdapter(new MyGallery(this));
		gallery.setSelection(2);

	}

	private View getImgView(int imgid) {
		image = new ImageView(this);
		switch (imgid % 5) {
		case 0:
			image.setImageResource(R.drawable.ad1);
			break;
		case 1:
			image.setImageResource(R.drawable.ad2);
			break;
		case 2:
			image.setImageResource(R.drawable.ad3);
			break;
		case 3:
			image.setImageResource(R.drawable.ad4);
			break;
		case 4:
			image.setImageResource(R.drawable.ad5);
			break;
		}
		return image;
	}

	public void setListener() {
		flipper.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				forwardRight(ProdDetail.class);
			}
		});
		find.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				forward(Search.class);
			}
		});
		find_bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				forwardRight(Search.class);
			}
		});
		miaosha_content.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				forwardZoom(ProdDetail.class);
			}
		});

		miaosha.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				miaosha_img = (ImageView) findViewById(R.id.miaosha_list_idex);
				if (miaosha_content.getVisibility() == View.GONE) {
					miaosha_content.setVisibility(View.VISIBLE);
					miaosha_img.setImageResource(R.drawable.index_icon3);
				} else {
					miaosha_content.setVisibility(View.GONE);
					miaosha_img.setImageResource(R.drawable.index_icon4);
				}

			}
		});

		qianggou.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				qianggou_img = (ImageView) findViewById(R.id.qianggou_list_idex);
				if (qianggou_content.getVisibility() == View.GONE) {

					qianggou_content.setVisibility(View.VISIBLE);
					qianggou_img.setImageResource(R.drawable.index_icon3);
				} else {

					qianggou_content.setVisibility(View.GONE);
					qianggou_img.setImageResource(R.drawable.index_icon4);
				}
			}
		});
		gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				forwardZoom(ProdDetail.class);
			}

		});

		// imageSwitcher.setOnTouchListener(new OnTouchListener() {
		//
		// @Override
		// public boolean onTouch(View v, MotionEvent event) {
		// // TODO Auto-generated method stub
		//
		// if (event.getAction() == MotionEvent.ACTION_DOWN) {
		//
		// downX = (int) event.getX(); // 取得按下时的坐标
		//
		// Log.i("event.getX()", " downX " + downX);
		// return true;
		// } else if (event.getAction() == MotionEvent.ACTION_UP) {
		//
		// upX = (int) event.getX(); // 取得松开时的坐标
		//
		// Log.i("event.getX()", " upX " + downX);
		//
		// // 从左拖到右，即看前一张
		// if (upX - downX > 10) {
		// imageSwitcher.setInAnimation(AnimationUtils
		// .loadAnimation(Copy_2_of_Home.this, R.anim.left_in));
		// imageSwitcher.setOutAnimation(AnimationUtils
		// .loadAnimation(Copy_2_of_Home.this, R.anim.right_out));
		// curIndex--;
		//
		// if (curIndex < 0) {
		// curIndex = imagelist.length - 1;
		// }
		//
		// imageSwitcher.setImageResource(imagelist[curIndex]);
		//
		// } else if (downX - upX > 10) { // 从右拖到左，即看后一张
		// imageSwitcher.setInAnimation(AnimationUtils
		// .loadAnimation(Copy_2_of_Home.this, R.anim.right_in));
		// imageSwitcher.setOutAnimation(AnimationUtils
		// .loadAnimation(Copy_2_of_Home.this, R.anim.left_out));
		// curIndex++;
		// if (curIndex > imagelist.length - 1) {
		// curIndex = 0;
		// }
		// imageSwitcher.setImageResource(imagelist[curIndex]);
		//
		// }
		// return true;
		// }
		// return false;
		// }
		// });

	}

	

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if (e1.getX() > e2.getX() + 10) {
			flipper.setInAnimation(AnimationUtils.loadAnimation(Home.this,
					R.anim.right_in));
			flipper.setOutAnimation(AnimationUtils.loadAnimation(Home.this,
					R.anim.left_out));
			flipper.showNext();
		} else if (e2.getX() > e1.getX() + 10) {
			flipper.setInAnimation(AnimationUtils.loadAnimation(Home.this,
					R.anim.left_in));
			flipper.setOutAnimation(AnimationUtils.loadAnimation(Home.this,
					R.anim.right_out));
			flipper.showPrevious();
		} else {
			return false;
		}
		return true;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		
		return false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return mGestureDetector.onTouchEvent(event);

	}

}
